package a66weding.com.jiehuntong.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.util.HashMap;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.beans.CommonResultBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.MarriageProposalBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.common.utils.CUtils;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.http.UrlDealUtil;

/**
 * Created by admin on 2016/7/19.
 */
public class ParticipantsActivity extends Activity implements View.OnClickListener,OnNetResultListener{
    private TextView back_iamge;
    private TextView title_tv;
    private MarriageProposalBean.DataBean dataBean;
    private TextView save;
    private String title;
    private EditText editText;
    private String content;
    private CommonResultBean been;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_participants);
        dataBean = (MarriageProposalBean.DataBean) getIntent().getSerializableExtra(JobDetail.TASK_FLAG);
        initView();
        initData();
    }
    private void initView(){
        back_iamge = (TextView) findViewById(R.id.back_iamge);
        title_tv = (TextView) findViewById(R.id.title_tv);
        save = (TextView) findViewById(R.id.activity_participants_save);
        save.setOnClickListener(this);
        editText = (EditText) findViewById(R.id.activity_participants_editext);
    }

    private void initData(){
        title_tv.setText("主要参与者");
        title = dataBean.getBookTitle();
        back_iamge.setText(title);
        editText.setText(dataBean.getParticipant()==null?"":dataBean.getParticipant());
    }

    public void back(View view){
        Intent intent  = new Intent();
        intent.putExtra(JobDetail.TASK_FLAG,dataBean);
        setResult(8,intent);
        finish();
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.activity_participants_save:
                update_participant();
                break;
        }

    }

    @Override
    public void onSucess(SuccessBean sucessbean) {
        Object data = sucessbean.getData();
        if(data instanceof  CommonResultBean){
            been = (CommonResultBean) data;
            switch (been.getResCode()){
                case "0000":
                    CUtils.showToast("修改主要助手成功");
                    dataBean.setParticipant(content);
                    Intent intent  = new Intent();
                    dataBean.setParticipant(dataBean.getParticipant());
                    intent.putExtra(JobDetail.TASK_FLAG,dataBean);
                    setResult(8,intent);
                    finish();
                    break;
            }
        }
    }

    @Override
    public void onError(ErrorBean errorBean) {

    }

    private void update_participant(){
        content = editText.getText().toString();
        String useUrl = UrlDealUtil.getRealUrl(RequestCode.UPDATE_PARTICIPANT);
        HashMap<String,String> paramsMap=new HashMap<>();
        paramsMap.put("BookTitle",title);
        paramsMap.put("Participant",content);
        HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, useUrl, this, CommonResultBean.class, RequestCode.UPDATE_PARTICIPANT, paramsMap);
        HttpHandleUtil.executeCommonPost(this,httpRequestBean);

    }
}
